﻿using System;
using System.Data;
using System.Data.Common;

namespace Songhay.Data.GenericWeb.Procedures
{
    /// <summary>
    /// Get procedures into the DBMS.
    /// </summary>
    public static partial class Get
    {
        /// <summary>
        /// Returns Fragment attribute the by the specified unique identifier.
        /// </summary>
        /// <param name="commonConnection">The common connection.</param>
        /// <param name="uniqueIdentifier">The unique identifier of the Fragment.</param>
        /// <param name="columnName">Name of the Fragment column (or attribute) containing the value.</param>
        /// <returns>Returns the <see cref="System.String"/> representation of the column value.</returns>
        public static string FragmentById(DbConnection commonConnection, string uniqueIdentifier, string columnName)
        {
            if(commonConnection == null) throw new ArgumentNullException("commonConnection", "The Common Connection is null.");
            if(uniqueIdentifier == null) throw new ArgumentNullException("uniqueIdentifier", "The unique identifer is null.");

            using(DbCommand cmd = commonConnection.CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = @"
SELECT
    *
FROM
    Fragment
WHERE
    fragmentName = @uniqueIdentifier
OR
    ClientId = @uniqueIdentifier
";
                DbParameter param = cmd.CreateParameter();
                param.ParameterName = "@uniqueIdentifier";
                param.Value = uniqueIdentifier;
                cmd.Parameters.Add(param);
                using(DbDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
                {
                    if(reader.Read())
                        return reader[columnName].ToString();
                    else
                        return null;
                }
            }
        }
    }
}
